package team.fourth.combat.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.fourth.combat.commen.entity.PageResult;
import team.fourth.combat.commen.entity.QueryPageBean;
import team.fourth.combat.dao.SetmealMapper;
import team.fourth.combat.pojo.CheckGroup;
import team.fourth.combat.pojo.CheckItem;
import team.fourth.combat.pojo.Setmeal;
import java.util.*;
import java.util.stream.Collectors;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void add(Setmeal setmeal) {
        //保存套餐基本信息返回主键
        setmealMapper.add(setmeal);
        //批量建立套餐和检查组的关系
        Integer setmealId = setmeal.getId();
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();

        setmealMapper.setSetmealAndCheckGroupBatch(setmealId,checkgroupIds);
    }

    @Override
    public List<Setmeal> getSetmeal() {
        return setmealMapper.getSetmeal();
    }

    @Override
    public Setmeal findById(Integer id) {
//        1、根据套餐id查询套餐详情
         Setmeal setmeal =   setmealMapper.findById(id);
          if(setmeal != null){
              //        2、根据套餐id查询套餐下面的所有检查组
              List<CheckGroup> checkGroups = setmealMapper.findCheckGroupBySetmealId(id);
              if(CollectionUtil.isNotEmpty(checkGroups)){
                  //        3、循环检查组查询对应的检查项集合
                  for (CheckGroup checkGroup : checkGroups) {
                      List<CheckItem> checkItems = setmealMapper.findCheckItemByCheckGroupId(checkGroup.getId());
                      checkGroup.setCheckItems(checkItems);
                  }
              }
              setmeal.setCheckGroups(checkGroups);

          }
         return setmeal;
    }

    @Override
    public Setmeal findByIdBatch(Integer id) {
//        1、根据套餐id查询套餐详情
        Setmeal setmeal =   setmealMapper.findById(id);
        if(setmeal != null){
            //        2、根据套餐id查询套餐下面的所有检查组
            List<CheckGroup> checkGroups = setmealMapper.findCheckGroupBySetmealId(id);
            if(CollectionUtil.isNotEmpty(checkGroups)){
//                //        3、循环检查组查询对应的检查项集合
//                for (CheckGroup checkGroup : checkGroups) {
//                    List<CheckItem> checkItems = setmealMapper.findCheckItemByCheckGroupId(checkGroup.getId());
//                    checkGroup.setCheckItems(checkItems);
//                }

                List<Integer> checkGroupIds = new ArrayList<>();
                for (CheckGroup checkGroup : checkGroups) {
                    checkGroupIds.add(checkGroup.getId());
                }

//                Map<Integer,List<CheckItem>> map = new HashMap<>();

                List<CheckItem> checkItems = setmealMapper.findCheckItemByCheckGroupIdBatch(checkGroupIds);
//                for (CheckGroup checkGroup : checkGroups) {
//                    List<CheckItem> items = new ArrayList<>();
//                    for (CheckItem checkItem : checkItems) {
//                        if(checkGroup.getId().equals(checkItem.getCheckGroupId())){
//                            items.add(checkItem);
//                        }
//                    }
//
//                    checkGroup.setCheckItems(items);
//                }

                Map<Integer,List<CheckItem>> map = new HashMap<>();

                /**for (CheckItem checkItem : checkItems) {
                    Integer checkGroupId = checkItem.getCheckGroupId();
                    List<CheckItem> items = map.get(checkGroupId);
                    if(items == null){k
                        items = new ArrayList<>();
                        items.add(checkItem);
                        map.put(checkGroupId,items);
                    } else {
                        items.add(checkItem);
                    }
                }*/

                map =  checkItems.stream().collect(Collectors.groupingBy(CheckItem::getCheckGroupId));

                for (CheckGroup checkGroup : checkGroups) {
                    List<CheckItem> items = map.get(checkGroup.getId());
                    checkGroup.setCheckItems(items);
                }

            }
            setmeal.setCheckGroups(checkGroups);

        }
        return setmeal;
    }

    @Override
    public Setmeal findDetailById(Integer id) {
        return  setmealMapper.findById(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Setmeal> setmeals = setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),setmeals);
    }
}
